package com.zhan.drugStore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhan.drugStore.bean.entity.Bill;
import com.zhan.drugStore.bean.entity.Customer;
import com.zhan.drugStore.bean.form.BillSearch;
import com.zhan.drugStore.bean.vo.BillVO;
import com.zhan.drugStore.dao.BillMapper;
import com.zhan.drugStore.service.BillService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhan.drugStore.service.CustomerService;
import com.zhan.drugStore.util.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zy
 * @since 2024-06-08
 */
@Service
public class BillServiceImpl extends ServiceImpl<BillMapper, Bill> implements BillService {

    @Resource
    private CustomerService customerService;
    @Override
    public void addOrUpdate(Bill bill) {
        //新增或更新账单
        this.saveOrUpdate(bill);
        //如果客户不存在，则新增客户,如果客户存在就更新客户信息
        Customer customer = BeanUtils.objToObj(bill, Customer.class);
        customerService.saveOrUpdate(customer);
    }

    @Override
    public List<BillVO> billList(BillSearch form) {
        LambdaQueryWrapper<Bill> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(form.getCustomerName()), Bill::getCustomerName, form.getCustomerName())
                .like(StringUtils.hasText(form.getIsDebt()), Bill::getIsDebt, form.getIsDebt())
                .ge(form.getBeginDate() != null, Bill::getEventTime, form.getBeginDate())
                .le(form.getEndDate() != null, Bill::getEventTime, form.getEndDate())
                .orderByDesc(Bill::getCreateDate);
        List<Bill> list = this.list(wrapper);
        return BeanUtils.objListToObjList(list, BillVO.class);
    }
}
